package com.ruoyi.zsyh.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修店档案Controller
 * 
 * @author yuexl
 * @date 2021-12-21
 */
@Controller
@RequestMapping("/zsyh/repairStore")
public class ZsyhUserRepairstoreController extends BaseController
{
    private String prefix = "zsyh/repairStore";

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhPlatformUserService zsyhPlatformUserService;

    @Autowired
    private IZsyhPlatformPartsService zsyhPlatformPartsService;

    @Autowired
    private IZsyhPlatformPartsCheckService zsyhPlatformPartsCheckService;

    @Autowired
    private IZsyhUserMessageService zsyhUserMessageService;

    @RequiresPermissions("zsyh:repairStore:view")
    @GetMapping()
    public String repairStore()
    {
        return prefix + "/repairStore";
    }

    @GetMapping("/networkStore")
    public String networkStore()
    {
        return prefix + "/networkStore";
    }@GetMapping("/appraisalStore")
    public String appraisalStore()
    {
        return prefix + "/appraisalStore";
    }

    @RequiresPermissions("zsyh:repairStore:view")
    @GetMapping("/repairStoreUnDeal")
    public String repairStoreUnDeal()
    {
        return prefix + "/repairStoreUnDeal";
    }

    @RequiresPermissions("zsyh:repairStore:view")
    @GetMapping("/bannerView")
    public String bannerView()
    {
        return prefix + "/bannerView";
    }

    @RequiresPermissions("zsyh:repairStore:view")
    @GetMapping("/electricalStore")
    public String electricalStore()
    {
        return prefix + "/electricalStore";
    }

    @GetMapping("/lockAgent/{id}")
    public String lockAgent(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("id",id);
        return prefix + "/lockAgent";
    }

    @GetMapping("/checkAppraisalStore/{id}")
    public String checkAppraisalStore(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("id",id);
        return prefix + "/reviewAppraisalStore";
    }

    @GetMapping("/updateRakerArea/{id}")
    public String updateRakerArea(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("id",id);
        return prefix + "/updateRakerArea";
    }

    /**
     * 查询维修店档案列表
     */
    @RequiresPermissions("zsyh:repairStore:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhUserRepairstore zsyhUserRepairstore)
    {
        startPage();
        List<ZsyhUserRepairstore> list = zsyhUserRepairstoreService.selectZsyhUserRepairstoreList(zsyhUserRepairstore);
        return getDataTable(list);
    }

    /**
     * 导出维修店档案列表
     */
    @RequiresPermissions("zsyh:repairStore:export")
    @Log(title = "维修店档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhUserRepairstore zsyhUserRepairstore)
    {
        List<ZsyhUserRepairstore> list = zsyhUserRepairstoreService.selectZsyhUserRepairstoreList(zsyhUserRepairstore);
        ExcelUtil<ZsyhUserRepairstore> util = new ExcelUtil<ZsyhUserRepairstore>(ZsyhUserRepairstore.class);
        return util.exportExcel(list, "维修店档案数据");
    }

    /**
     * 新增维修店档案
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存维修店档案
     */
    @RequiresPermissions("zsyh:repairStore:add")
    @Log(title = "维修店档案", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhUserRepairstore zsyhUserRepairstore)
    {
        //判断手机师傅已经存在
        ZsyhUserRepairstore zsyhUserRepairstore1 = new ZsyhUserRepairstore();
        zsyhUserRepairstore1.setStorePhone(zsyhUserRepairstore.getStorePhone());
        if(zsyhUserRepairstoreService.selectZsyhUserRepairstoreList(zsyhUserRepairstore1).size()>0){
            return error("该手机号已经存在!");
        }else{
            return toAjax(zsyhUserRepairstoreService.insertZsyhUserRepairstore(zsyhUserRepairstore));
        }
    }

    /**
     * 修改维修店档案
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        mmap.put("zsyhUserRepairstore", zsyhUserRepairstore);
        return prefix + "/edit";
    }

    /**
     * 修改网点档案
     */
    @GetMapping("/viewRepairStore/{id}")
    public String viewRepairStore(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        mmap.put("zsyhUserRepairstore", zsyhUserRepairstore);
        return prefix + "/viewRepairStore";
    }

    /**
     * 修改鉴定点档案
     */
    @GetMapping("/viewAppraisalStore/{id}")
    public String viewAppraisalStore(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        mmap.put("zsyhUserRepairstore", zsyhUserRepairstore);
        return prefix + "/viewAppraisalStore";
    }

    /**
     * 修改网点档案
     */
    @GetMapping("/viewNetworkStore/{id}")
    public String viewNetworkStore(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        mmap.put("zsyhUserRepairstore", zsyhUserRepairstore);
        return prefix + "/viewNetworkStore";
    }

    /**
     * 修改保存维修店档案
     */
    @Transactional
    @RequiresPermissions("zsyh:repairStore:edit")
    @Log(title = "维修店档案", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhUserRepairstore zsyhUserRepairstore)
    {
        ZsyhUserRepairstore zsyhUserRepairstore1 = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(zsyhUserRepairstore.getId());
        ZsyhPlatformUser zsyhPlatformUser = new ZsyhPlatformUser();
        zsyhPlatformUser.setId(zsyhUserRepairstore1.getUserId());

        zsyhUserRepairstore.setZsyhUserRepairareaList(zsyhUserRepairstore1.getZsyhUserRepairareaList());
        if("2".equals(zsyhUserRepairstore.getStoreState())){
            //通过
            zsyhPlatformUser.setIsCity("1");
            zsyhPlatformUserService.updateZsyhPlatformUser(zsyhPlatformUser);
            return toAjax(zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(zsyhUserRepairstore));
        }else{

            return toAjax(zsyhUserRepairstoreService.updateZsyhUserRepairstoreType(zsyhUserRepairstore));
        }
        /*if("3".equals(zsyhUserRepairstore.getStoreState())){
            //驳回
            zsyhUserRepairstore.setIsRaker("0");
        }*/
    }

    /**
     * 删除维修店档案
     */
    @RequiresPermissions("zsyh:repairStore:remove")
    @Log(title = "维修店档案", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(ids);
        //删除店铺，调整用户角色
        int num = zsyhUserRepairstoreService.deleteZsyhUserRepairstoreByIds(ids);
        if(num >0){
            ZsyhPlatformUser zsyhPlatformUser =  new ZsyhPlatformUser();
            zsyhPlatformUser.setId(zsyhUserRepairstore.getUserId());
            zsyhPlatformUser.setUserType("1");
            zsyhPlatformUserService.updateZsyhPlatformUser(zsyhPlatformUser);
        }
        return toAjax(num);
    }

    /**
     * 删除鉴定点档案
     */
    @Transactional
    @Log(title = "删除鉴定点档案", businessType = BusinessType.DELETE)
    @PostMapping( "/removeAppraisal")
    @ResponseBody
    public AjaxResult removeAppraisal(String ids)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(ids);
        //删除店铺，调整用户角色
        zsyhUserRepairstore.setIsAppraisal("0");
        zsyhUserRepairstore.setAppraisalState("0");
        int num = zsyhUserRepairstoreService.updateZsyhUserRepairstore(zsyhUserRepairstore);
        //修改用户
        ZsyhPlatformUser zsyhPlatformUser = zsyhPlatformUserService.selectZsyhPlatformUserById(zsyhUserRepairstore.getUserId());
        zsyhPlatformUser.setIsAppraisal("0");
        zsyhPlatformUserService.updateZsyhPlatformUser(zsyhPlatformUser);
        return toAjax(num);
    }

    /**
     * 删除网点档案
     */
    @Transactional
    @Log(title = "删除网点档案", businessType = BusinessType.DELETE)
    @PostMapping( "/removeNetwork")
    @ResponseBody
    public AjaxResult removeNetwork(String ids)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(ids);
        //删除店铺，调整用户角色
        zsyhUserRepairstore.setIsRaker("0");
        zsyhUserRepairstore.setStoreState("0");
        int num = zsyhUserRepairstoreService.updateZsyhUserRepairstore(zsyhUserRepairstore);
        //修改用户
        ZsyhPlatformUser zsyhPlatformUser = zsyhPlatformUserService.selectZsyhPlatformUserById(zsyhUserRepairstore.getUserId());
        zsyhPlatformUser.setIsCity("0");
        zsyhPlatformUserService.updateZsyhPlatformUser(zsyhPlatformUser);
        return toAjax(num);
    }

    /**
     * 关闭配件鉴定权
     */
    @Log(title = "关闭配件鉴定权", businessType = BusinessType.INSERT)
    @PostMapping("/closePartsCheck")
    @ResponseBody
    public AjaxResult closePartsCheck(String id,String type)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        if("1".equals(type)){
            zsyhUserRepairstore.setIsAppraisal("0");
        }else if("2".equals(type)){
            zsyhUserRepairstore.setIsAppraisal("1");
        }
        return toAjax(zsyhUserRepairstoreService.updateZsyhUserRepairstore(zsyhUserRepairstore));
    }

    /**
     * 审核店铺
     */
    @GetMapping("/checkStore/{id}")
    public String checkStore(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        mmap.put("zsyhUserRepairstore", zsyhUserRepairstore);
        return prefix + "/checkStore";
    }

    /**
     * 审核店铺
     */
    @GetMapping("/updateStore/{id}")
    public String updateStore(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        mmap.put("zsyhUserRepairstore", zsyhUserRepairstore);
        return prefix + "/updateStore";
    }


    /**
     * 置顶店铺
     */
    @RequiresPermissions("zsyh:partsStore:add")
    @Log(title = "置顶店铺", businessType = BusinessType.INSERT)
    @PostMapping("/topStore")
    @ResponseBody
    public AjaxResult topStore(String id,String value)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        zsyhUserRepairstore.setIsDiamond(value);
        return toAjax(zsyhUserRepairstoreService.updateZsyhUserRepairstore(zsyhUserRepairstore));
    }

    /**
     * 审核鉴定点
     */
    @Transactional
    @Log(title = "审核鉴定点")
    @PostMapping("/checkAppraisalStore")
    @ResponseBody
    public AjaxResult checkAppraisalStore(String appraisalRejectReason,String id,String type)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        zsyhUserRepairstore.setAppraisalRejectReason(appraisalRejectReason);
        zsyhUserRepairstore.setAppraisalState(type);
        if("2".equals(type)){
            //通过
            ZsyhPlatformUser zsyhPlatformUser = zsyhPlatformUserService.selectZsyhPlatformUserById(zsyhUserRepairstore.getUserId());
            zsyhPlatformUser.setIsAppraisal("1");
            zsyhPlatformUserService.updateZsyhPlatformUser(zsyhPlatformUser);
            /*//将所以产品的鉴定权进行发放
            ZsyhPlatformParts zsyhPlatformParts = new ZsyhPlatformParts();
            zsyhPlatformParts.setPartsHasGrant("1");
            List<ZsyhPlatformParts> zsyhPlatformPartsList = zsyhPlatformPartsService.selectZsyhPlatformPartsList(zsyhPlatformParts);
            List<ZsyhPlatformPartsCheck> zsyhPlatformPartsCheckList = new ArrayList<ZsyhPlatformPartsCheck>();
            for(ZsyhPlatformParts temp : zsyhPlatformPartsList){
                ZsyhPlatformPartsCheck zsyhPlatformPartsCheck = new ZsyhPlatformPartsCheck();
                zsyhPlatformPartsCheck.setStoreId(id);
                zsyhPlatformPartsCheck.setPartsId(temp.getId());
                zsyhPlatformPartsCheckList.add(zsyhPlatformPartsCheck);
            }
            //批量添加
            zsyhPlatformPartsCheckService.insertZsyhPlatformPartsCheckBatch(zsyhPlatformPartsCheckList);*/
        }else{

        }
        /*if("3".equals(type)){
            //驳回
            zsyhUserRepairstore.setIsAppraisal("0");
        }*/
        return toAjax(zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(zsyhUserRepairstore));
    }

    @Log(title = "绑定会长", businessType = BusinessType.INSERT)
    @PostMapping("/lockAgent")
    @ResponseBody
    public AjaxResult lockAgent(String storeId,String operatorId)
    {
        ZsyhUserRepairstore zsyhUserRepairstore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(storeId);
        zsyhUserRepairstore.setAgentId(operatorId);
        return toAjax(zsyhUserRepairstoreService.updateZsyhUserRepairstore(zsyhUserRepairstore));
    }

}
